//package gmu.swe.project.peer;
//
//
//import java.awt.BorderLayout;
//import java.awt.event.ActionEvent;
//import java.awt.event.ActionListener;
//import java.awt.event.WindowAdapter;
//import java.awt.event.WindowEvent;
//
//import java.util.Observable;
//import java.util.Observer;
//import java.util.Random;
//
//import javax.swing.BorderFactory;
//import javax.swing.JButton;
//import javax.swing.JFrame;
//import javax.swing.JLabel;
//
//import javax.swing.JPanel;
//import javax.swing.JProgressBar;
//import javax.swing.JScrollPane;
//import javax.swing.JTable;
//import javax.swing.JTextField;
//import javax.swing.ListSelectionModel;
//import javax.swing.event.ListSelectionEvent;
//import javax.swing.event.ListSelectionListener;
//
//public class TestPeer extends JFrame
//implements Observer {
//
///**
//	 * 
//	 */
//	private static final long serialVersionUID = 1L;
//
//// Add download text field.
//private JTextField ipText, portText, filenameText;
//
//// Download table's data model.
//private DownloadsTable tableModel;
//
//// Table listing downloads.
//private JTable table;
//
//// These are the buttons for managing the selected download.
//private JButton pauseButton, resumeButton;
//private JButton cancelButton, clearButton;
//
//// Currently selected download.
//private DownloaderThread selectedDownload;
//private UploaderService uploader;
//
//// Flag for whether or not table selection is being cleared.
//private boolean clearing;
//
//String q = null,ip,file = null;
//int port, uploadPort, noOfUploads = 0;
//JLabel uploads = new JLabel(Integer.toString(noOfUploads));
//
//
//
//
//// Constructor for Download Manager.
//public TestPeer() {
//
//	uploadPort = 3000+new Random().nextInt(5000);
//	uploader = new UploaderService(uploadPort);
//
//	uploader.addObserver(TestPeer.this);
//	
//	setTitle("Hybrid P2P File Sharing System");
//	
//	setSize(640, 480);
//	
//	addWindowListener(new WindowAdapter() {
//	    public void windowClosing(WindowEvent e) {
//	        actionExit();
//	    }
//	});
//	
//	// Set up add panel.
//	JPanel addPanel = new JPanel();
//	ipText = new JTextField(10);
//	portText = new JTextField(10);
//	filenameText = new JTextField(10);
//	JLabel ipLabel = new JLabel("IP: ");
//	JLabel portLabel = new JLabel("Port: ");
//	JLabel filenameLabel = new JLabel("File: ");
//	addPanel.add(ipLabel);
//	addPanel.add(ipText);
//	addPanel.add(portLabel);
//	addPanel.add(portText);
//	addPanel.add(filenameLabel);
//	addPanel.add(filenameText);
//	JButton addButton = new JButton("Download");
//	addButton.addActionListener(new ActionListener() {
//	    public void actionPerformed(ActionEvent e) {
//	        actionAdd();
//	    }
//	});
//	addPanel.add(addButton);
//	addPanel.add(uploads);
//	
//	// Set up Downloads table.
//	tableModel = new DownloadsTable();
//	table = new JTable(tableModel);
//	table.getSelectionModel().addListSelectionListener(new
//	        ListSelectionListener() {
//	    public void valueChanged(ListSelectionEvent e) {
//	        tableSelectionChanged();
//	    }
//	});
//	// Allow only one row at a time to be selected.
//	table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//	
//	// Set up ProgressBar as renderer for progress column.
//	ProgressRenderer renderer = new ProgressRenderer(0, 100);
//	renderer.setStringPainted(true); // show progress text
//	table.setDefaultRenderer(JProgressBar.class, renderer);
//	
//	// Set table's row height large enough to fit JProgressBar.
//	table.setRowHeight(
//	        (int) renderer.getPreferredSize().getHeight());
//	
//	// Set up downloads panel.
//	JPanel downloadsPanel = new JPanel();
//	downloadsPanel.setBorder(
//	        BorderFactory.createTitledBorder("Downloads"));
//	downloadsPanel.setLayout(new BorderLayout());
//	downloadsPanel.add(new JScrollPane(table),
//	        BorderLayout.CENTER);
//	
//	// Set up buttons panel.
//	JPanel buttonsPanel = new JPanel();
//	pauseButton = new JButton("Pause");
//	pauseButton.addActionListener(new ActionListener() {
//	    public void actionPerformed(ActionEvent e) {
//	        actionPause();
//	    }
//	});
//	pauseButton.setEnabled(false);
//	buttonsPanel.add(pauseButton);
//	resumeButton = new JButton("Resume");
//	resumeButton.addActionListener(new ActionListener() {
//	    public void actionPerformed(ActionEvent e) {
//	        actionResume();
//	    }
//	});
//	resumeButton.setEnabled(false);
//	buttonsPanel.add(resumeButton);
//	cancelButton = new JButton("Cancel");
//	cancelButton.addActionListener(new ActionListener() {
//	    public void actionPerformed(ActionEvent e) {
//	        actionCancel();
//	    }
//	});
//	cancelButton.setEnabled(false);
//	buttonsPanel.add(cancelButton);
//	clearButton = new JButton("Clear");
//	clearButton.addActionListener(new ActionListener() {
//	    public void actionPerformed(ActionEvent e) {
//	        actionClear();
//	    }
//	});
//	clearButton.setEnabled(false);
//	buttonsPanel.add(clearButton);
//	
//	// Add panels to display.
//	getContentPane().setLayout(new BorderLayout());
//	getContentPane().add(addPanel, BorderLayout.NORTH);
//	getContentPane().add(downloadsPanel, BorderLayout.CENTER);
//	getContentPane().add(buttonsPanel, BorderLayout.SOUTH);
//	}
//	
//	// Exit this program.
//	private void actionExit() {
//	System.exit(0);
//	}
//	
//	// Add a new download.
//	private void actionAdd() {
//	    tableModel.addDownload(new DownloaderThread(ipText.getText(), Integer.parseInt(portText.getText()), filenameText.getText()));
//	
//	}
//	
//	// Called when table row selection changes.
//	private void tableSelectionChanged() {
//	/* Unregister from receiving notifications
//	from the last selected download. */
//	if (selectedDownload != null)
//	    selectedDownload.deleteObserver(TestPeer.this);
//	
//	/* If not in the middle of clearing a download,
//	set the selected download and register to
//	receive notifications from it. */
//	if (!clearing) {
//	    selectedDownload =
//	            tableModel.getDownload(table.getSelectedRow());
//	    selectedDownload.addObserver(TestPeer.this);
//	    updateButtons();
//	}
//	}
//	
//	// Pause the selected download.
//	private void actionPause() {
//	selectedDownload.pause();
//	updateButtons();
//	}
//	
//	// Resume the selected download.
//	private void actionResume() {
//	selectedDownload.resume();
//	updateButtons();
//	}
//	
//	// Cancel the selected download.
//	private void actionCancel() {
//	selectedDownload.cancel();
//	updateButtons();
//	}
//	
//	// Clear the selected download.
//	private void actionClear() {
//	clearing = true;
//	tableModel.clearDownload(table.getSelectedRow());
//	clearing = false;
//	selectedDownload = null;
//	updateButtons();
//	}
//	
//	/* Update each button's state based off of the
//	currently selected download's status. */
//	private void updateButtons() {
//	if (selectedDownload != null) {
//	    int status = selectedDownload.getStatus();
//	    switch (status) {
//	        case 0:
//	            pauseButton.setEnabled(true);
//	            resumeButton.setEnabled(false);
//	            cancelButton.setEnabled(true);
//	            clearButton.setEnabled(false);
//	            break;
//	        case 1:
//	            pauseButton.setEnabled(false);
//	            resumeButton.setEnabled(true);
//	            cancelButton.setEnabled(true);
//	            clearButton.setEnabled(false);
//	            break;
//	        case 4:
//	            pauseButton.setEnabled(false);
//	            resumeButton.setEnabled(true);
//	            cancelButton.setEnabled(false);
//	            clearButton.setEnabled(true);
//	            break;
//	        default: // COMPLETE or CANCELLED
//	            pauseButton.setEnabled(false);
//	            resumeButton.setEnabled(false);
//	            cancelButton.setEnabled(false);
//	            clearButton.setEnabled(true);
//	    }
//	} else {
//	    // No download is selected in table.
//	    pauseButton.setEnabled(false);
//	    resumeButton.setEnabled(false);
//	    cancelButton.setEnabled(false);
//	    clearButton.setEnabled(false);
//	}
//	}
//	
//	/* Update is called when a Download notifies its
//	observers of any changes. */
//	public void update(Observable o, Object arg) {
//	// Update buttons if the selected download has changed.
//	if (selectedDownload != null && selectedDownload.equals(o))
//	    updateButtons();
//	
//	
//	uploads.setText(Integer.toString(uploader.UploadsCount()));
//	}
//	
//	// Run the Download Manager.
//	public static void main(String[] args) {
//	TestPeer manager = new TestPeer();
//	manager.setVisible(true);
//	}
//}
//    
//	
//	
//	/*public static void main(String[] args) throws IOException {
//
//        String q = null,ip,file = null;
//        int port, uploadPort=3000+new Random().nextInt(5000);
//        
//       System.out.println("Attempting to launch upload service on port "+uploadPort);
//      
//       new UploaderService(uploadPort).start();
//       System.out.println("Upload service successfully started on port "+uploadPort);
//       
//       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//       do {
//       try {
//       	System.out.print("IP: ");
//       	ip = br.readLine();
//       	System.out.print("Port: ");
//       	port = Integer.parseInt(br.readLine());
//       	System.out.print("File: ");
//       	file = br.readLine();
//       	
//       	System.out.println(ip+port+file);
//       	new DownloaderServiceThread(ip,port,file).start();
//       	
//       	System.out.print("Would you like to download a file (y/n): ");
//       	q = br.readLine();
//        } catch (IOException ioe) {
//           System.out.println("IO error!");
//           System.exit(1);
//        }
//       }while(q != "n");
//       
//       System.exit(1);
//   }*/
//
//
